

#include <idt.h>

struct __idt_descriptor __idt_desc;

struct __idt_entry __idt[256];

extern void __idt_0();
extern void __idt_1();
extern void __idt_2();
extern void __idt_3();
extern void __idt_4();
extern void __idt_5();
extern void __idt_6();
extern void __idt_7();
extern void __idt_8();
extern void __idt_9();
extern void __idt_10();
extern void __idt_11();
extern void __idt_12();
extern void __idt_13();
extern void __idt_14();
extern void __idt_15();
extern void __idt_16();
extern void __idt_17();
extern void __idt_18();
extern void __idt_19();
extern void __idt_20();
extern void __idt_21();
extern void __idt_22();
extern void __idt_23();
extern void __idt_24();
extern void __idt_25();
extern void __idt_26();
extern void __idt_27();
extern void __idt_28();
extern void __idt_29();
extern void __idt_30();
extern void __idt_31();

void __make_idt_entry(unsigned char index, unsigned int base,
		      unsigned short selector, unsigned char flags){

  __idt[index].offset_lo = base & 0xffff;
  __idt[index].offset_hi = (base >> 16) & 0xffff;

  __idt[index].selector = selector;
  __idt[index].flags = flags;
  __idt[index].zero = 0;

}

void __set_default_idt(){

  __make_idt_entry(0, (unsigned int) __idt_0, 0x08, 0x8e);
  __make_idt_entry(1, (unsigned int) __idt_1, 0x08, 0x8e);
  __make_idt_entry(2, (unsigned int) __idt_2, 0x08, 0x8e);
  __make_idt_entry(3, (unsigned int) __idt_3, 0x08, 0x8e);
  __make_idt_entry(4, (unsigned int) __idt_4, 0x08, 0x8e);
  __make_idt_entry(5, (unsigned int) __idt_5, 0x08, 0x8e);
  __make_idt_entry(6, (unsigned int) __idt_6, 0x08, 0x8e);
  __make_idt_entry(7, (unsigned int) __idt_7, 0x08, 0x8e);
  __make_idt_entry(8, (unsigned int) __idt_8, 0x08, 0x8e);
  __make_idt_entry(9, (unsigned int) __idt_9, 0x08, 0x8e);
  __make_idt_entry(10, (unsigned int) __idt_10, 0x08, 0x8e);
  __make_idt_entry(11, (unsigned int) __idt_11, 0x08, 0x8e);
  __make_idt_entry(12, (unsigned int) __idt_12, 0x08, 0x8e);
  __make_idt_entry(13, (unsigned int) __idt_13, 0x08, 0x8e);
  __make_idt_entry(14, (unsigned int) __idt_14, 0x08, 0x8e);
  __make_idt_entry(15, (unsigned int) __idt_15, 0x08, 0x8e);
  __make_idt_entry(16, (unsigned int) __idt_16, 0x08, 0x8e);
  __make_idt_entry(17, (unsigned int) __idt_17, 0x08, 0x8e);
  __make_idt_entry(18, (unsigned int) __idt_18, 0x08, 0x8e);
  __make_idt_entry(19, (unsigned int) __idt_19, 0x08, 0x8e);
  __make_idt_entry(20, (unsigned int) __idt_20, 0x08, 0x8e);
  __make_idt_entry(21, (unsigned int) __idt_21, 0x08, 0x8e);
  __make_idt_entry(22, (unsigned int) __idt_22, 0x08, 0x8e);
  __make_idt_entry(23, (unsigned int) __idt_23, 0x08, 0x8e);
  __make_idt_entry(24, (unsigned int) __idt_24, 0x08, 0x8e);
  __make_idt_entry(25, (unsigned int) __idt_25, 0x08, 0x8e);
  __make_idt_entry(26, (unsigned int) __idt_26, 0x08, 0x8e);
  __make_idt_entry(27, (unsigned int) __idt_27, 0x08, 0x8e);
  __make_idt_entry(28, (unsigned int) __idt_28, 0x08, 0x8e);
  __make_idt_entry(29, (unsigned int) __idt_29, 0x08, 0x8e);
  __make_idt_entry(30, (unsigned int) __idt_30, 0x08, 0x8e);
  __make_idt_entry(31, (unsigned int) __idt_31, 0x08, 0x8e);

  int i;
  for (i = 32; i < 256; i++){
    __make_idt_entry(i, 0, 0, 0);
  }

}

void __idt_init(){

  __idt_desc.base = (unsigned int)&__idt;
  __idt_desc.lim = sizeof(struct __idt_entry) * 8;
  __load_idt();

}

void __isr_handle(int interrupt, int err_code){

  char err_num[12];
  itos(interrupt, err_num, 12);
  k_print("Exception caught: ");
  k_print(err_num);
  k_printl();
  k_print(" Error Code: ");
  char err_cod[12];
  itos(err_code, err_cod, 12);
  k_print(err_cod);
  k_printl();

  for (;;);

}
